package cn.com.huzo.service.master.impl;

import cn.com.huzo.model.entities.master.MaterialGroupMemberUserEntity;
import huzofw.service.impl.GenericManagerImpl;
import huzofw.model.entities.paginator.Paginator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.apache.commons.collections4.MapUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.com.huzo.service.master.MaterialGroupMemberUserManager;
import cn.com.huzo.repository.master.MaterialGroupMemberUserRepository;

/**
 * 材料组成员户管理服务对象类实现。
 *
 * @author 吴旭
 */
@Service("MaterialGroupMemberUserManager")
public class MaterialGroupMemberUserManagerImpl extends GenericManagerImpl<MaterialGroupMemberUserEntity, HashMap>
        implements MaterialGroupMemberUserManager {

    @Resource(name = "MaterialGroupMemberUserRepository")
    private MaterialGroupMemberUserRepository _dao;

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    public Map<String, Object> fetchForPage(Map<String, Object> criterion) throws Exception {
        Map<String, Object> rtnValue = new HashMap();

        Long offset = (long) (MapUtils.getInteger(criterion, "Limit") * (MapUtils.getInteger(criterion, "PageIndex") - 1));
        long total = _dao.fetchForCount(criterion);

        if (total > 0) {
            if (offset >= total) {
                offset = (total - MapUtils.getInteger(criterion, "Limit")) < 0 ? 0 : (total - MapUtils.getInteger(criterion, "Limit"));
            }
            criterion.put("Offset", offset);
            List<MaterialGroupMemberUserEntity> itemList = _dao.fetchForPage(criterion);
            Paginator<MaterialGroupMemberUserEntity> paginator = new Paginator<>(itemList, offset, MapUtils.getInteger(criterion, "Limit"), total);

            rtnValue.put("TotalRecordCount", paginator.getTotal());
            rtnValue.put("TotalPages", paginator.getTotalPages());
            rtnValue.put("PageIndex", paginator.getPageIndex());
            rtnValue.put("DataList", paginator.getItemList());
            rtnValue.put("Success", true);
        } else {
            rtnValue.put("Success", false);
        }

        return rtnValue;
    }

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    public Map<String, Object> batchInsert(List<MaterialGroupMemberUserEntity> itemList, Integer limit, Integer pageIndex) throws Exception {
        Map<String, Object> _result = new HashMap();

        Integer rowsAffected = _dao.batchInsert(itemList);
        if (rowsAffected > 0) {
            Map<String, Object> criterion = new HashMap();
            criterion.put("GroupId", itemList.get(0).getMaterialGroupId());
            criterion.put("Limit", limit);
            criterion.put("PageIndex", pageIndex);
            _result = this.fetchForPage(criterion);
        }
        return _result;
    }

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    public Map<String, Object> deleteLines(Long groupId, String groupName, Long memberId, String memberName,
            Long deletedBy, Integer limit, Integer pageIndex) throws Exception {
        Map<String, Object> _result = new HashMap();
        Map<String, Object> criterion = new HashMap();
        criterion.put("EmployeeId", memberId);
        criterion.put("GroupId", groupId);
        Integer rowsAffected = _dao.delete(criterion);

        if (rowsAffected > 0) {
            criterion.clear();
            criterion.put("GroupId", groupId);
            criterion.put("Limit", limit);
            criterion.put("PageIndex", pageIndex);
            _result = this.fetchForPage(criterion);
        }

        return _result;
    }

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    public Map<String, Object> batchDelete(List<MaterialGroupMemberUserEntity> itemList) throws Exception {
        Map<String, Object> _result = new HashMap();
        Integer rowsAffected = 0;
        Map<String, Object> criterion = new HashMap();
        for (int i = 0; i < itemList.size(); i++) {
            MaterialGroupMemberUserEntity instance = itemList.get(i);
            criterion.clear();
            criterion.put("EmployeeId", instance.getEmployeeId());
            criterion.put("GroupId", instance.getMaterialGroupId());
            Integer _rows = _dao.delete(criterion);
            rowsAffected = rowsAffected + _rows;
            if (_rows > 0) {
                // 审计日志
                writeAuditLog(instance.getDeletedBy(), "基础信息→材料组用户",
                        "（用户：" + instance.getEmployeeName() + "）",
                        "删除");
            }
        }
        
        if (rowsAffected > 0) {
            _result.put("Success", true);
        } else {
            _result.put("Success", false);
        }
        return _result;
    }
}
